package cn.tedu.csmall.product.controller;


import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.product.pojo.param.AttributeTemplateAddNewParam;
import cn.tedu.csmall.product.pojo.param.AttributeTemplateUpdateInfoParam;
import cn.tedu.csmall.product.pojo.vo.*;
import cn.tedu.csmall.product.service.IAttributeTemplateService;
import cn.tedu.csmall.commons.web.JsonResult;
import com.github.pagehelper.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/attribute-template")
@Api(tags = "06,属性模板管理模块")
@Slf4j
@Validated
public class AttributeTemplateController {

    @Autowired
    private IAttributeTemplateService attributeTemplateService;

    @PostMapping("/add-new")
    @PreAuthorize("hasAuthority('/pms/product/add-new')")
    @ApiOperation("添加属性模板")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(AttributeTemplateAddNewParam attributeTemplateAddNewParam){
        log.debug("开始处理【添加属性模板】的请求,参数：{}", attributeTemplateAddNewParam);
        attributeTemplateService.addNew(attributeTemplateAddNewParam);
        return JsonResult.success();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @PreAuthorize("hasAuthority('/pms/product/delete')")
    @ApiOperation("根据ID删除属性模板")
    @ApiOperationSupport(order = 200)

    public JsonResult delete(@Range(min = 1 , message = "根据ID删除属性模板失败，请提交合法的ID值")
        @RequestParam Long id){
        log.debug("开始处理[根据ID删除属性模板]的请求，参数：{}",id);
        attributeTemplateService.delete(id);
        return JsonResult.deleteSuccess();

    }

    @PostMapping("/{id:[0-9]+}/update")
    @PreAuthorize("hasAuthority('/pms/product/update')")
    @ApiOperation("修改属性模板")
    @ApiOperationSupport(order = 300)
    public JsonResult update(@RequestParam @Range(min = 1, message = "请提交有效的属性模板ID值！") Long id,
                             @Valid AttributeTemplateUpdateInfoParam attributeTemplateUpdateInfoParam){
        log.debug("开始处理[修改属性模板详情]的业务,参数:{}",attributeTemplateUpdateInfoParam);

        attributeTemplateService.updateById(id,attributeTemplateUpdateInfoParam);
        return JsonResult.updatesuccess();
    }

    @GetMapping("")
    @PreAuthorize("hasAuthority('/pms/product/read')")
    @ApiOperation("查询属性模板列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "页码",paramType = "query"),
            @ApiImplicitParam(name = "queryType",value = "查询类型",paramType = "all")

    }
    )
    public JsonResult list(@Range(min = 1,message = "查询属性模板列表失败，请提供正确的页码值！") Integer page,
                           String queryType){
        log.debug("开始处理[查询属性模板列表]的请求，页码：{}",page);

        Integer pageNum = page == null ? 1: page;
        PageData<AttributeTemplateListItemVO> pageData;

        if("all".equals(queryType)){
            pageData = attributeTemplateService.list(pageNum,Integer.MAX_VALUE);
        }
        else{
            pageData = attributeTemplateService.list(pageNum);
        }


        return JsonResult.success(pageData);
    }



    @GetMapping("/{id:[0-9]+}")
    @PreAuthorize("hasAuthority('/pms/product/read')")
    @ApiOperation("根据ID查询属性模板详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "属性模板ID", required = true, dataType = "long")
    })
    public JsonResult getStandardById(@Range(min = 1,message = "根据ID查询属性模板详情失败，请提交合法的ID值！")
                                      @RequestParam Long id){
        log.debug("开始处理[根据ID查询属性模板详情]的请求，参数:{}",id);
        AttributeTemplateStandardVO result = attributeTemplateService.getStandardById(id);

        return JsonResult.success(result);
    }


}
